﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Angle.Krill
{
    internal static class Program
    {
        private static ApplicationContext context = null;
        private static bool isAutoStart = false;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            WelcomeForm wForm = new WelcomeForm();
            wForm.Show();
            context = new ApplicationContext();
            context.Tag = wForm;

            isAutoStart = args.Length > 0;

            Application.Idle += new EventHandler(Application_Idle);

            Application.Run(context);
        }

        private static void Application_Idle(object sender, EventArgs e)
        {
            Application.Idle -= new EventHandler(Application_Idle);

            if (context.MainForm == null)
            {
                Main main = new Main();
                context.MainForm = main;

                Thread.Sleep(500);

                WelcomeForm wForm = (WelcomeForm)context.Tag;
                wForm.Close();

                main.Show();
                if (isAutoStart)
                {
                    main.NotifyIcon.Visible = true;    //显示托盘图标
                    main.Hide();
                }
            }
        }
    }
}
